package com.ws.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ws.bean.User;
import com.ws.common.CommonResult;
import com.ws.service.UserService;
import com.ws.util.JWTUtil;

@RestController
public class LoginController {
	
	@Autowired
	UserService service;
	@PostMapping("dologin")
	public CommonResult doLogin(@RequestBody User user) {
		String token = service.doLogin(user);
		if(token != null) {
			return CommonResult.success(200,"登录成功",token);
		}
		return CommonResult.fail(403,"登录失败");
	}
	@GetMapping("checktoken")
	public CommonResult checkToken(HttpServletRequest request) {
		String token = request.getHeader("Authorization");
		if(token != null && JWTUtil.verifyToken(token)) {
			return CommonResult.success();
		}
		return CommonResult.fail();
	}
}
